﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Clothes.Items.ModuleData;
using System.Windows.Forms;
using ApplicationLibrary;

namespace Clothes.Items.Libraries
{
    class CategoriesTree : System.Windows.Forms.TreeView
    {
        public string Code { get; set; }

        private RUCategoriesByFather ruCategories = null;
        private RUItemsContainsCategory ruItemsCat = null;

        public void LoadCategories(string father = "", TreeNode node = null)
        {
            if (ruCategories == null)
                ruCategories = new RUCategoriesByFather();

            if (node == null)
            {
                SuspendLayout();
                Nodes.Clear();
            }

            ruCategories.Find(father);
            TreeNodeCollection curNode = node != null
                                ? node.Nodes
                                : Nodes;

            for (int t = 0; t < ruCategories.Count; t++)
                curNode.Add(ruCategories.GetValue<string>(CL_Categories.Code, t), ruCategories.GetValue<string>(CL_Categories.Description, t));

            for (int t = 0; t < curNode.Count; t++)
                LoadCategories(curNode[t].Name, curNode[t]);

            if (node == null)
            {
                SelectCurrentCode(Code, node);
                ResumeLayout();
            }
        }

        private void SelectCurrentCode(string code, TreeNode node = null)
        {
            TreeNodeCollection curNode = node != null
                    ? node.Nodes
                    : Nodes;

            for (int t = 0; t < curNode.Count; t++)
                if (curNode[t].Name == code)
                {
                    SelectedNode = curNode[t];
                    SelectedNode.EnsureVisible();
                }
                else
                    if (curNode[t].Nodes.Count > 0)
                        SelectCurrentCode(code, curNode[t]);
        }

        public TreeNode FindCurrentCode(string code, TreeNode node = null)
        {
            TreeNodeCollection curNode = node != null
                    ? node.Nodes
                    : Nodes;

            for (int t = 0; t < curNode.Count; t++)
                if (curNode[t].Name == code)
                    return curNode[t];
                else
                    if (curNode[t].Nodes.Count > 0)
                    {
                        TreeNode findNode = FindCurrentCode(code, curNode[t]);
                        if (findNode != null)
                            return findNode;
                    }

            return null;
        }

        public string CreateCode(TreeNode node)
        {
            string Code = string.Empty;
            TreeNode curNode = node;
            do
            {
                if (!Code.IsEmpty())
                    Code = "-" + Code;

                Code = string.Format("{0, 2}{1}", curNode.Name, Code);
                curNode = curNode.Parent;
            }
            while (curNode != null);
            return Code;
        }

        public void MovedCode(TreeNode node)
        {
            if (ruItemsCat == null)
                ruItemsCat = new RUItemsContainsCategory(true);

            ruItemsCat.Find(node.Name);
            for (int t = 0; t < ruItemsCat.Count; t++)
            {
                string cat = ruItemsCat.GetValue<string>(CL_Items.Category, t);
                cat = cat.Substring(cat.Length - 2);

                TreeNode curnode = FindCurrentCode(cat);
                if (curnode != null)
                {
                    string newcode = CreateCode(curnode);
                    ruItemsCat.SetValue<string>(CL_Items.Category, t, newcode);
                    ruItemsCat.Update();
                }
            }
        }
    }
}
